 /**
 * Description: 生成业务表字段vo
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-18 下午 14:31:31
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-18   xk       0.0.1     Initial
 */
package com.metro.generator.gen.beans.vo;

 import com.metro.generator.gen.beans.entity.GenTableColumnEntity;
 import com.metro.generator.gen.beans.constant.JavaType;
 import com.metro.utils.StringUtil;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 /**
 * 生成业务表字段vo<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-18]
 */
@Data()
@EqualsAndHashCode(callSuper = false)
public class GenTableColumn extends GenTableColumnEntity {

     public boolean isDateColumn(){
         return JavaType.DATE.name().equalsIgnoreCase(super.getJavaType());
     }

     public boolean isStringColumn(){
         return JavaType.STRING.name().equalsIgnoreCase(super.getJavaType());
     }

     public String getJavaFieldCapitalize(){
         return StringUtil.capitalize(this.getJavaField());
     }

     /**
      * 返回数据字典类的名
      * @return
      */
     public String getDicTypeShort(){
         if(StringUtil.isNotEmpty(this.getDicType())){
             if(this.getDicType().indexOf("com.") == 0){
                 String dic = this.getDicType().substring(0, this.getDicType().lastIndexOf("."));
                 return this.getDicType().substring(dic.lastIndexOf(".") + 1);
             }
             return this.getDicType();
         }
         return null;
     }

     /**
      * 返回数据字典包名
      * @return
      */
     public String getDicTypePackage(){
         if(StringUtil.isNotEmpty(this.getDicType())){
             if(this.getDicType().indexOf("com.") == 0){
                 return this.getDicType().substring(0, this.getDicType().lastIndexOf("."));
             }
             return this.getDicType();
         }
         return null;
     }

     /**
      * 根据当前字段的数据字典类型，和表单类型返回 javaField 或者javaField + Str
      * <p>1. 数据字典类型不为空，或者是日期就返回 javaField + Str</p>
      * <p>2. 其他情况返回 javaField</p>
      * @return
      */
     public String getJavaFieldStr(){
         if(StringUtil.isNotEmpty(this.getDicType())
                 || isDateColumn()){
             return this.getJavaField() +"Str";
         }

         return this.getJavaField();
     }

     /**
      * 列的数据库备注的简短形式
      * @return
      */
     public  String getColumnRemarkShort() {
         String columnAlias = null;
         if(super.getColumnRemark() != null){
             columnAlias = super.getColumnRemark().trim();
             int index = columnAlias.indexOf(" ");
             if(index < 1){
                 index = columnAlias.indexOf(":");
             }
             if(index < 1){
                 index = columnAlias.indexOf("：");
             }
             if(index < 1){
                 index = columnAlias.indexOf("(");
             }
             if(index < 1){
                 index = columnAlias.indexOf("[");
             }
             if(index > 0){
                 return columnAlias.substring(0, index);
             }else{
                 return columnAlias;
             }
         }
         return "";
     }

     /**
      * 拼接jquery-validate校验字符串
      * @return name : { required:true, maxlength:200}
      */
     public String getValidateString(){
         StringBuilder sb = new StringBuilder();
         if(this.getIsRequire()){
             sb.append(", required : true");
         }
         if("Integer".equalsIgnoreCase(this.getJavaType())
                 || "Long".equalsIgnoreCase(this.getJavaType())){
             sb.append(", digits : true ");
         }else if("Float".equalsIgnoreCase(this.getJavaType())
                 || "Double".equalsIgnoreCase(this.getJavaType())){
             sb.append(", number : true ");
         }else if("String".equalsIgnoreCase(this.getJavaType())){
             int start = this.getColumnType().indexOf("(");
             int end = this.getColumnType().indexOf(")");
             if(start > 0 && end > 0){
                 String length = this.getColumnType().substring(start + 1, end);
                 sb.append(", maxlength : ").append(length);
             }

         }
         if(sb.length() > 0){
             sb.deleteCharAt(0).insert(0, this.getJavaField()+" : {")
                     .append(" }");
         }
         return sb.toString();
     }


}
